package com.binvshe.binvshe.mine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.Account;
import com.binvshe.binvshe.account.AccountManager;
import com.binvshe.binvshe.account.ui.LoginActivity;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.baseuicomponent.activity.BaseActivity;

import butterknife.Bind;

public class MyCoinActivity extends BaseActivity {

    @Bind(R.id.tv_coin)
    TextView mTvCoin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar();
        initData();
    }

    private void initData() {
        if(TextUtils.isEmpty(AccountManager.getCurrentAccount().getUserId())){
            LoginActivity.start(MyCoinActivity.this);
        }else{
            String requestUrl = String.format(UrlCenter.USER, new Object[]{AccountManager.getCurrentAccount().getUserId()});
            MyHttpRequest request = new MyHttpRequest(MyCoinActivity.this);
            request.get(requestUrl, null, new DataRequestListener<Account>(Account.class) {
                @Override
                public void onStart(Context context, String url) {
                    super.onStart(context, url);
                }

                @Override
                public void onSuccess(Account o) {
                    super.onSuccess(o);
                    mTvCoin.setText(o.getCoin()+"");
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                }
            });
        }



    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, MyCoinActivity.class);
        activity.startActivity(intent);
    }

    private void initToolBar() {
        setTitle("我的萌都币");
        getCustomToolbar().addLeftImageButton(R.mipmap.icon_back_coin, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getCustomToolbar().addRightButton("查看明细", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CoinDetailActivity.start(MyCoinActivity.this);
            }
        });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_my_coin, null);
    }
}
